package basic.thread.juc.chapter4;

import java.util.concurrent.CountDownLatch;

public class TestCountDownLatch {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(5);
        CountDownLatchDemo countDownLatchDemo = new CountDownLatchDemo(countDownLatch);
        for(int i=0;i<5;i++){
            new Thread(countDownLatchDemo).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("运行耗时:"+(endTime-startTime)+"ms");
    }
}

class CountDownLatchDemo implements Runnable{

    private CountDownLatch countDownLatch;

    public CountDownLatchDemo(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try{
             for(int i=0;i<50000;i++){
                if(i%2==0){
                    System.out.println(i);
                }
             }
        }finally {
            countDownLatch.countDown();
        }
    }
}
